<template>
	<view>
		<view class="p-3">
			<view class="bt-block">
				<bg-cell2 label="项目名称">
					<template slot="value">
						<view class="form-block">
							<bg-select :value.sync="postData.projectId" :columns="projectList"
								key="projectId"></bg-select>
						</view>
					</template>
				</bg-cell2>
				<bg-cell2 label="保险成本金额" :border="false">
					<template slot="value">
						<u-input inputAlign='right' type="number" border="none" v-model="postData.insuranceAmount">
							<template slot="suffix">
								<text>元</text>
							</template>
						</u-input>
					</template>
				</bg-cell2>
			</view>
			<view class="bt-block mt-3">
				<bg-cell2 label="上传电子保险单" :border="false">
					<template slot="extra">
						<u--image @click="open" width="76" height="76" src="/static/images/ui/bg/upload2.png"></u--image>
						<view v-if="selectFile.name">
							<view  class="my-3 font-1" style="width: 300rpx;">
								{{selectFile.name}}
							</view>
							<view class="font-1">文件上传成功</view>
						</view>
						
					</template>
				</bg-cell2>
			</view>
		</view>
		<!-- <web-view :update-title="false" :src="pdfUrl" :fullscreen="false"></web-view> -->
		<view class="bt-footer">
			<bg-btn @click="saveData">保存</bg-btn>
		</view>
	</view>
</template>

<script>
	import {insuranceAdd, insuranceDetail, insuranceEdit} from "@/api/apply.js"
	import upload from "@/utils/upload.js"
	// var AfDocument = uni.requireNativePlugin("Aq-ChooseFile");
	const plugin = uni.requireNativePlugin('GW-ChooseFileModule')

	export default {
		onLoad(options){
			this.id = options.id
			if(this.id){
				this.getData()
				uni.setNavigationBarTitle({
					title:"编辑采购保险"
				})
			}
			
		},
		created(){
			// #ifdef APP-PLUS
			// var currentWebview = this.$mp.page.$getAppWebview(); //获取当前web-view
			// setTimeout(function() {
			// var wv = currentWebview.children()[0];
			// wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度，单位为px
			// 		top: 320,
			// 		bottom:90
			// 	})
			// }, 1000);
			// #endif
		},
		mounted() {
			this.$store.dispatch("types/getProjects").then(res => {
				this.projectList = res
			})
		},
		data() {
			return {
				id:'',
				projectList: [],
				postData: {
					"projectId": 0,
					"insurancePolicy": "",
					"insuranceAmount": 0
				},
				uploadFile:{},
				selectFile:{},
				pdfUrl:''
			}
		},
		methods: {
			getData(){
				insuranceDetail(this.id).then(res=>{
					this.postData = res.data
					this.pdfUrl = '/hybrid/html/web/viewer.html?file='+encodeURIComponent(res.data.insurancePolicy)
				})
			},
			open() {
				// AfDocument.openMode({
				// 	size: '1', //选择总数量
				// 	paths: ['/storage/emulated/0/Download', '/storage/emulated/0/A', ], //自定义选择目录
				// 	isDown: true, //是否下钻（true 筛选当前目录以下的所有文件，fales 只筛选当前目录文件） 
				// 	types: [{
				// 		name: '文档',
				// 		value: ["pdf"]
				// 	}]
				// }, (res) => {
				// 	this.selectFile = res.res[0]
				// 	upload({filePath:this.selectFile.pathHolp}).then(res=>{
				// 		this.uploadFile = res.data
				// 		this.postData.insurancePolicy = res.data.fileName
				// 	})
				// })
				plugin.chooseFile(
				    {
				        count: 1,
				        extension: ["pdf"],
				        // 非必填，指定目录，可指定任意多个目录
				        paths: [
				            {
				                deep: true, // deep为true表示扫描文件包含指定目录及以下的全部子目录，false表示只扫描指定目录下一级的文件
				                path: 'Download' // 指定根目录下的相对目录，不用写根目录（/storage/emulated/0）
				            },
				            {
				                deep: true,
				                path: 'Documents'
				            }
				        ]
				    },
					res=>{ 
						this.selectFile = res.files[0]
						uni.showLoading({
							title:"上传中"
						})
						upload({filePath:this.selectFile.path}).then(res=>{
							console.log(res.data)
							this.uploadFile = res.data
							this.postData.insurancePolicy = res.data.fileName;
							uni.hideLoading()
						}).catch(()=>{
							uni.hideLoading()
						})
					})
			},
			saveData(){
				if(!this.postData.projectId){
					this.$modal.msg("请选择项目")
					return false
				}
				if(!this.postData.insuranceAmount){
					this.$modal.msg("请输入保险成本金额")
					return false
				}
				if(!this.postData.insurancePolicy){
					this.$modal.msg("请上传电子版保险单")
					return false
				}
				if(this.id){
					insuranceEdit(this.postData).then(res=>{
						this.$modal.msgSuccess(res.msg)
						uni.$emit("refresh-page", true)
						this.$tab.navigateBackDelay()
					})	
				}else{
					insuranceAdd(this.postData).then(res=>{
						console.log(res)
						this.$modal.msgSuccess(res.msg)
						uni.$emit("refresh-page", true)
						this.$tab.navigateBackDelay()
					})
				}
				
			}
		}
	}
</script>

<style>

</style>